<?php
namespace app\api\validate;

use think\Validate;

class Dict extends Validate
{
    // 字典类型验证规则
    protected $rule = [
        'dict_name' => 'require|max:100',
        'dict_type' => 'require|max:100',
        'status'    => 'require|in:0,1',
        'remark'    => 'max:500',
    ];

    protected $message = [
        'dict_name.require' => '字典名称不能为空',
        'dict_name.max'     => '字典名称长度不能超过100个字符',
        'dict_type.require' => '字典类型不能为空',
        'dict_type.max'     => '字典类型长度不能超过100个字符',
        'dict_type.unique'  => '字典类型已存在',
        'status.require'    => '状态不能为空',
        'status.in'         => '状态值不正确',
        'remark.max'        => '备注长度不能超过500个字符',
    ];

    // 字典数据验证规则
    protected $dictDataRule = [
        'dict_type'  => 'require',
        'dict_label' => 'require|max:100',
        'dict_value' => 'require|max:100',
        'dict_sort'  => 'require|number',
        'status'     => 'require|in:0,1',
        'css_class'  => 'max:100',
        'list_class' => 'max:100',
        'is_default' => 'in:Y,N',
        'remark'     => 'max:500',
    ];

    protected $dictDataMessage = [
        'dict_type.require'  => '字典类型不能为空',
        'dict_label.require' => '字典标签不能为空',
        'dict_label.max'     => '字典标签长度不能超过100个字符',
        'dict_value.require' => '字典键值不能为空',
        'dict_value.max'     => '字典键值长度不能超过100个字符',
        'dict_sort.require'  => '显示排序不能为空',
        'dict_sort.number'   => '显示排序必须为数字',
        'status.require'     => '状态不能为空',
        'status.in'          => '状态值不正确',
        'css_class.max'      => '样式属性长度不能超过100个字符',
        'list_class.max'     => '回显样式长度不能超过100个字符',
        'is_default.in'      => '是否默认值不正确',
        'remark.max'         => '备注长度不能超过500个字符',
    ];

    /**
     * 字典类型场景
     */
    public function sceneTypeAdd()
    {
        return $this->only(['dict_name','dict_type','status','remark']);
    }

    public function sceneTypeEdit()
    {
        return $this->remove('dict_type', 'unique')
            ->only(['dict_name','dict_type','status','remark']);
    }

    /**
     * 字典数据场景
     */
    public function sceneDataAdd()
    {
        return $this->rule($this->dictDataRule)
            ->message($this->dictDataMessage)
            ->only(['dict_type','dict_label','dict_value','dict_sort','status','css_class','list_class','is_default','remark']);
    }

    public function sceneDataEdit()
    {
        return $this->rule($this->dictDataRule)
            ->message($this->dictDataMessage)
            ->remove('dict_label', 'unique')
            ->only(['dict_type','dict_label','dict_value','dict_sort','status','css_class','list_class','is_default','remark']);
    }
}